package com.example.ymm.modules1.controller.user;

import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.my.user.DbCategory;
import com.example.ymm.modules1.entity.my.user.DbGood;
import com.example.ymm.modules1.entity.my.user.DbOrder;
import com.example.ymm.modules1.entity.my.user.DbOrderItem;
import com.example.ymm.modules1.query.DbCategoryQueryCriteria;
import com.example.ymm.modules1.query.DbGoodQueryCriteria;
import com.example.ymm.modules1.query.DbOrderQueryCriteria;
import com.example.ymm.modules1.service.user.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/hl/user/good")
@Api(tags = "用户端 商品和订单管理")
@Slf4j
public class UserGoodController extends BaseController {

    @Autowired
    private DbGoodService dbGoodService;
    @Autowired
    DbCategoryService dbCategoryService;

    @Autowired
    DbOrderService dbOrderService;



    @ApiOperation(value = "商品列表")
    @RequestMapping(value = "/goodList", method = RequestMethod.POST)
    public CommonResult goodList(@Valid DbGoodQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<DbGood> list = dbGoodService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "商品详情")
    @RequestMapping(value = "/goodDetail", method = RequestMethod.POST)
    public CommonResult goodDetail(@ApiParam(value = "数据主键") @RequestParam(required = true) String goodId) {
        try{
            DbGood good = dbGoodService.getById(goodId);
            return CommonResult.success(good);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "所有分类")
    @RequestMapping(value = "/categoryAll", method = RequestMethod.POST)
    public CommonResult categoryAll(DbCategoryQueryCriteria criteria) {
        try{
            List<DbCategory> dbCategories = dbCategoryService.queryAll(criteria);
            return CommonResult.success(dbCategories);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "立刻购买")
    @RequestMapping(value = "/buyNow", method = RequestMethod.POST)
    public CommonResult buyNow(@Valid DbOrderItem orderItem) {
        try{
            dbOrderService.buyNow(orderItem);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "订单取消")
    @RequestMapping(value = "/orderCancel", method = RequestMethod.POST)
    public CommonResult orderCancel(String orderId) {
        try{
            dbOrderService.userOrderCancel(orderId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "订单详情")
    @RequestMapping(value = "/orderDetail", method = RequestMethod.POST)
    public CommonResult orderDetail(String orderId) {
        try{
            dbOrderService.detail(orderId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "订单列表")
    @RequestMapping(value = "/orderList", method = RequestMethod.POST)
    public CommonResult orderList(@Valid DbOrderQueryCriteria criteria, YmPageDto ymPage) {
        try{
            String userId = getUserId();
            criteria.setUserId(userId);
            TableList<DbOrder> list = dbOrderService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "取货")
    @RequestMapping(value = "/takesOrder", method = RequestMethod.POST)
    public CommonResult takesOrder(String orderId) {
        try{
            dbOrderService.takesOrder(orderId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

}
